﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>btable</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/static/plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="/static/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/static/css/btable.css" />
    <script src="/static/js/inout/jquery-1.8.2.min.js"></script>
</head>

<body style=" background-color: gainsboro;">

    <div style="margin:0px; background-color: white; margin:0 10px;">
        <blockquote class="layui-elem-quote" style="height: 31px;">
            <button type="button" class="layui-btn layui-btn-small" id="add"><i class="fa fa-plus" aria-hidden="true"></i> 添加</button>
            <form class="layui-form" style="float:right;" action="/admin/classification/bicthmomo" method="post">
                <div class="layui-form-item" style="margin:0;">
                    <label class="layui-form-label">搜索</label>
                    <div class="layui-input-inline" style="width: 220px;">
                        <input type="text" name="username" placeholder="输入类型名字" value="" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux" style="padding:0;">
                        <button lay-filter="search" class="layui-btn" lay-submit type="submit"><i class="fa fa-search" aria-hidden="true"></i> 查询</button>
                    </div>
                </div>
            </form>
        </blockquote>
        <div id="content" style="width: 100%;height: 533px;"></div>
        <div id="demo7"></div>
    </div>

    <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
    <script>
        layui.config({
            base: '/static/js/',
            v: new Date().getTime()
        }).use(['btable', 'form','paging'], function () {
            var btable = layui.btable(),
                $ = layui.jquery,
                layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
                layer = layui.layer,//获取当前窗口的layer对象;
                form = layui.form();

            btable.set({
                openWait: true,//开启等待框
                elem: '#content',
                url: '/admin/classification/bicthmomo', //数据源地址
                pageSize: 10,
                params: {
                    t: new Date().getTime() //额外的请求参数
                },
                columns: [{ //配置数据列
                    fieldName: '类型标题', //显示名称
                    field: 'titlename', //字段名
                    sortable: true //是否显示排序
                },{
                    fieldName: '创建时间',
                    field: 'addTime',
                    sortable: true
                },{
                    fieldName: '修改时间',
                    field: 'updateTime',
                    format: function (id, obj) {
                        //id
                        //行数据对象
                        //返回值：格式化的纯文本或html文本
                        return obj.sign;
                    }
                },{
                    fieldName: '操作',
                    field: 'id',
                    format: function (val,obj) {
                        var html = '<input type="button" value="修改" id="edit" data-action="edit" data-title="'+obj.titlename+'" data-bd="'+obj.curl+'" data-id="'+val+'" class="layui-btn layui-btn-mini" />' +
                                '<input type="button" value="删除" data-action="del" data-id="'+val+'" class="layui-btn layui-btn-mini layui-btn-danger del" name="del" />';
                        return html;
                    }
                }],
                even: true,//隔行变色
                field: 'id', //主键ID
                //skin: 'row',
                checkbox: false,//是否显示多选框
                paged: true, //是否显示分页
                singleSelect: false, //只允许选择一行，checkbox为true生效
                onSuccess: function ($elem) { //$elem当前窗口的jq对象
                    $elem.children('tr').each(function () {
                        $(this).children('td:last-child').children('input').each(function () {
                            var $that = $(this);
                            var action = $that.data('action');
                            var id = $that.data('id');
                            var title = $that.data('title');
                            var curl = $that.data('bd');
                            $that.on('click', function () {
                                switch (action) {
                                    case 'edit':
                                        var cons = '<div style="padding:10px;"><input type="hidden" value="'+id+'" class="layui-input" style="margin-bottom:5px;" id="claid"/>' +
                                                '<input type="text" class="layui-input" style="margin-bottom:5px;" id="titlename" value="'+title+'" placeholder="分类标题" />' +
                                                '<input type="text" class="layui-input" id="urls" value="'+curl+'" placeholder="前台域名"/></div>';
                                        layer.open({
                                            type: 1,
                                            title: '修改分类',
                                            content: cons,
                                            btn: ['确定', '取消'],
                                            area: ['350px', '220px'],
                                            yes: function (index, layero) {
                                                //这是核心的代码。
                                                var claid = $("#claid").val();
                                                var titlename = $("#titlename").val();
                                                var urls = $("#urls").val();
                                                if(titlename==''){
                                                    layer.msg("密码不能为空");
                                                    return false;
                                                }

                                                $.ajax({
                                                    type:"post",
                                                    dataType: "json",
                                                    url: "/admin/classification/edit",
                                                    data:{
                                                        titlename:titlename,
                                                        claid:claid,
                                                        urls:urls
                                                    },
                                                    success:function(res){
                                                        if(res.status==0){
                                                            layer.msg(res.msg);
                                                            setTimeout('location.href="/admin/classification/index"',1000);
                                                        }else{
                                                            layer.msg(res.msg);
                                                        }
                                                    }
                                                });

                                            },
                                            maxmin: true
                                        });

                                        break;
                                    case 'del': //删除
                                        //询问框
                                        layer.confirm('确定要删除？', {
                                            btn: ['确定','取消'] //按钮
                                        }, function(){
                                            var claid = $that.attr('data-id');
                                            $.ajax({
                                                type:"post",
                                                dataType: "json",
                                                url: "/admin/classification/del",
                                                data:{
                                                    claid:claid
                                                },
                                                success:function(res){
                                                    if(res.status==0){
                                                        layer.msg(res.msg,{icon: 1});
                                                        setTimeout('location.href="/admin/classification/index"',1000);
                                                    }else{
                                                        layer.msg(res.msg,{icon: 1});
                                                    }
                                                }
                                            });
                                        }, function(){

                                        });
                                        break;
                                }
                            });
                        });
                    });
                }
            });

            btable.render();
            //监听搜索表单的提交事件
            form.on('submit(search)', function (data) {
                btable.get(data.field);
                return false;
            });
            $(window).on('resize', function (e) {
                var $that = $(this);
                $('#content').height($that.height() - 92);
            }).resize();
        });
        //上面那个是页面渲染完就添加的
        layui.use(['layer'], function () {
            var $ = layui.jquery,
                    layer = layui.layer;

            //移除当前的选项卡
            $('#remove').on('click', function () {
                var tab = parent.tab;

                tab.deleteTab(tab.getCurrentTabId());
            });

            //这个是点击之后才添加的。
            $('#add').on('click', function () {
                var con = '<div style="padding:10px;"><input type="text" class="layui-input" style="margin-bottom:5px;" id="titlename" placeholder="分类标题" />' +
                        '</div>';

                layer.open({
                    type: 1,
                    title: '添加分类标题',
                    content: con,
                    btn: ['确定', '取消'],
                    area: ['350px', '170px'],
                    yes: function (index, layero) {
                        //这是核心的代码。
                        var titlename = $("#titlename").val();
                        if(titlename==''){
                            layer.msg("用户名不能为空");
                            return false;
                        }
                        $.ajax({
                            type:"post",
                            dataType: "json",
                            url: "/admin/classification/add",
                            data:{
                                titlename:titlename
                            },
                            success:function(res){
                                if(res.status==0){
                                    layer.msg(res.msg);
                                    setTimeout('location.href="/admin/classification/index"',1000);
                                }else{
                                    layer.msg(res.msg);
                                }
                            }
                        });
                    },
                    maxmin: true
                });

            });
        });
    </script>
</body>

</html>